#!/bin/bash

set -e

function usage() {
    echo "Usage: $( basename $0 ) [-d <local_dir> | -h | -a | -u]"
    echo "      用纯git准备caffe量化工具gerrit开发目录"
    echo "      命令行参数" 
    echo "      -d <local_dir> 工作目录, required" 
    echo "      -a 建立data,models符号链接时, 使用绝对路径。缺省使用相对路径"
    echo "      -u 配置用户仓库"
    echo "      -h 输出本信息"
    exit 1
}   

branch="master"
gitlabRepo="http://gitlab.tsingmicro.com/algorithm/quant-tool-dev/caffe/quantize.git"
gerritRepo="http://192.168.100.107/quantize"

absLink=0
userRepo=0
while getopts "d:hau" arg
do
    case $arg in
        d)
            localDir="$OPTARG"
            ;;
        a)
            absLink=1
            ;;
        u)
            userRepo=1
            ;;
        *)
            usage
            ;;
    esac
done

if [ -z "${localDir}" ]
then
    usage
fi

if [ -f "${localDir}" ]
then
    echo "${localDir}必须是个目录"
    exit 1
fi

if [ ! -e "${localDir}" ]
then
    echo mkdir -p "${localDir}"
    mkdir -p "${localDir}"
fi

# clone并设置仓库
cd "${localDir}"
git clone "${gitlabRepo}" -b "${branch}"
cd quantize
git remote remove origin
git remote add origin "${gerritRepo}"

# 按命令行要求增加用户仓库
userRepoUrl="http://gitlab.tsingmicro.com/liujinsong/quantize.git"
userRepoName="ljs"
if [ 1 -eq "${userRepo}" ]
then
    git remote add "${userRepoName}" "${userRepoUrl}"
fi

# 建立数据和模型的链
cd ..
if [ 1 -eq "${absLink}" ]
then
    myln.sh "${HOME}/data" data
    myln.sh "${HOME}/models" models
else
    dp=$( realpath --relative-to . ${HOME}/data )
    myln.sh "${dp}" data
    mp=$( realpath --relative-to . ${HOME}/models )
    myln.sh "${mp}" models
fi

